use "ppc_clean.dta", replace
*----------------------*
* Multiple imputations *
*----------------------*
mi set wide
mi register imputed cps21_covid_sat_1-pes21_parents_born
mi register imputed ppc
set seed 890315
mi impute chained (pmm, knn(5)) ppc cps21_covid_sat_1-pes21_parents_born, add(5) burnin(50) savetrace(bsptrace_pmm.dta, replace) 
save "ppc_mi_nonstd.dta", replace

* standardize all covariates
use "ppc_mi_nonstd.dta", replace
center cps21_covid_sat_1-pes21_parents_born, addtolabel("")  standardize 
drop cps21_covid_sat_1-pes21_parents_born
foreach x of varlist c_cps21_covid_sat_1-c_pes21_parents_born{
	renvars `x', predrop(2)
}

center _1_cps21_covid_sat_1-_5_pes21_parents_born, addtolabel("")  standardize 
drop _1_cps21_covid_sat_1-_5_pes21_parents_born
foreach x of varlist c__1_cps21_covid_sat_1-c__5_pes21_parents_born{
	renvars `x', predrop(2)
}
* saving the dataset after multiple imputations
save "ppc_mi.dta", replace

*-----------------------------------------------------*
* Diagnostic of convergence (Figures B.1.1 to B.1.14) *
*-----------------------------------------------------*
use "bsptrace_pmm.dta", replace // retrieving the traceplots
set scheme white_ptol
reshape long @_mean @_sd, i(iter m) j(var) string
* Build means over iterations
renpfix _ est
bys iter var: egen mean_mean = mean(estmean)
bys iter var: egen mean_sd = mean(estsd)

* For each m data set separately
reshape long est, i(iter m var) j(meansd) string
separate est, by(m)

*********************************************
* Pandemic-related attitudes and behaviours *
*********************************************
local graphname ""
foreach x in cps21_covid_sat_1 cps21_covid_sat_2 cps21_covid_sat_3 cps21_vaccine_mandat_1 cps21_vaccine_mandat_2 cps21_vaccine_mandat_3  {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "1a_pandemic.png", replace 


local graphname ""
foreach x in cps21_vaccine1 cps21_covid_liberty cps21_comfort cps21_covidrelief  {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "1b_pandemic.png", replace 



*********************************
* Health status self-assessment *
*********************************
local graphname ""
foreach x in pes21_health pes21_phealth pes21_mhealth {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "2_health status.png", replace 

********************************
* Anti-establishment attitudes *
********************************
local graphname ""
foreach x in cps21_demsat pes21_dem_sat cps21_fed_gov_sat cps21_prov_gov_sat pes21_keepromises pes21_losetouch {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "3a_anti_estab.png", replace 


local graphname ""
foreach x in pes21_conf_inst1_1 pes21_conf_inst1_2 pes21_conf_inst1_3 pes21_conf_inst1_4 pes21_conf_inst2_1 pes21_conf_inst2_2  {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "3b_anti_estab.png", replace 


local graphname ""
foreach x in pes21_conf_inst2_3 pes21_conf_inst2_4 pes21_conf_inst2_5 pes21_conf_inst2_6 pes21_populism_2 pes21_populism_3  {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "3c_anti_estab.png", replace 

local graphname ""
foreach x in pes21_populism_4 pes21_populism_6 pes21_populism_7 pes21_populism_8 cps21_pol_eth{
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "3d_anti_estab.png", replace 


*****************************************************
* Efficacy, political interest, political knowledge *
*****************************************************
local graphname ""
foreach x in cps21_interest_gen_1 cps21_interest_elxn_1 cps21_govt_confusing cps21_govt_say  pes21_govtcare cps21_polknow {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "4_eff_polint_know.png", replace 

****************
* Social trust *
****************
tw line est? iter if var=="pes21_trust", by(var meansd, yrescale legend(off) cols(1) note(""))
graph export "5_social trust.png", replace 


******************************************************************
* Left/right placement of parties and ideological self-placement *
******************************************************************
local graphname ""
foreach x in cps21_lr_parties_1 cps21_lr_parties_2 cps21_lr_parties_3 cps21_lr_parties_4  {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "6a_left_right.png", replace 

local graphname ""
foreach x in cps21_lr_parties_5 cps21_lr_parties_7 cps21_lr_scale_bef_1 {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "6b_left_right.png", replace 

***************************************************************************************
* Experience of spoiling votes and reason for choosing the party in the 2021 election *
***************************************************************************************
local graphname ""
foreach x in cps21_spoil pes21_reason_chose_1 pes21_reason_chose_2 pes21_reason_chose_3 pes21_reason_chose_4 pes21_reason_chose_5 {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "7_spoil_reason.png", replace 



*************************************
* Political information consumption *
*************************************
local graphname ""
foreach x in cps21_news_cons  pes21_follow_pol  pes21_discfam pes21_campatt {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "8_pol_info.png", replace 

***************************
* Political participation *
***************************
local graphname ""
foreach x in cps21_volunteer pes21_partic1_1 pes21_partic1_2 pes21_partic1_3 pes21_partic1_4 pes21_partic2_1  {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "9a_pol_part.png", replace 

local graphname ""
foreach x in pes21_partic2_2 pes21_partic2_3 pes21_partic2_4 pes21_partic3_1  {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "9b_pol_part.png", replace 

local graphname ""
foreach x in pes21_partic3_2 pes21_partic3_3 pes21_partic3_4 pes21_partymember {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "9c_pol_part.png", replace 

*******************************
* Big Five personality traits *
*******************************
local graphname ""
foreach x in pes21_big5_1 pes21_big5_2 pes21_big5_3 pes21_big5_4 pes21_big5_5 pes21_big5_6 {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "10a_big5.png", replace 

local graphname ""
foreach x in pes21_big5_7 pes21_big5_8 pes21_big5_9 pes21_big5_10 {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "10b_big5.png", replace 

**********************
* Cultural attitudes *
**********************
local graphname ""
foreach x in pes21_envirojob pes21_pos_carbon pes21_cc1 pes21_pos_energy cps21_spend_env cps21_spend_rec_indi   {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "11a_cultural.png", replace 

local graphname ""
foreach x in cps21_imm pes21_fitin pes21_immigjobs cps21_refugees cps21_groups_therm_1 cps21_groups_therm_2 {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "11b_cultural.png", replace 

local graphname ""
foreach x in cps21_groups_therm_3 cps21_groups_therm_4 cps21_groups_therm_6  pes21_groups1_1 pes21_groups1_2 pes21_groups1_3 {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "11c_cultural.png", replace 

local graphname ""
foreach x in pes21_equalrights pes21_bilingualism  pes21_qclang pes21_qcsol pes21_ab_favors pes21_donerm {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "11d_cultural.png", replace 


local graphname ""
foreach x in pes21_donew pes21_donegl pes21_doneqc pes21_langQC pes21_cultureQC pes21_abort2 {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "11e_cultural.png", replace 


local graphname ""
foreach x in pes21_hostile2 pes21_hostile4 pes21_newerlife pes21_conversion_the {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "11f_cultural.png", replace 

************
* Identity *
************
local graphname ""
foreach x in pes21_ethid_1 pes21_ethid_2 pes21_ethid_3  {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "12a_identity.png", replace 

local graphname ""
foreach x in pes21_can_id_1 pes21_can_id_2 pes21_can_id_3 pes21_can_id_4 pes21_can_id_5 pes21_can_id_6 {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "12b_identity.png", replace 


*****************************************************************
*  Economic attitudes, economic perceptions and economic status *
*****************************************************************
local graphname ""
foreach x in cps21_spend_educ cps21_spend_defence  cps21_econ_retro cps21_econ_fed_bette cps21_own_fin_retro cps21_ownfinanc_fed  {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "13a_econ.png", replace    

local graphname ""
foreach x in cps21_own_fin_future pes21_privjobs pes21_blame pes21_inequal pes21_gap  pes21_stdofliving{
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "13b_econ.png", replace 

****************
* Demographics *
****************
local graphname ""
foreach x in cps21_bornin_canada cps21_education pes21_rural_urban pes21_lived   {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "14a_demo.png", replace 

local graphname ""
foreach x in cps21_children cps21_union pes21_parents_born {
	tw line est? iter if var=="`x'", by(var meansd, yrescale legend(off) cols(1) note(""))
	graph save `x', replace 
	local graphname "`graphname' `x'.gph"
}
graph combine `graphname'
graph export "14b_demo.png", replace    
